#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QMouseEvent>
#include <QDebug>
#include <QRect>
#include <QKeyEvent>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    speed = 15;
    updateRedAreaPosition();
    createScoreBox();
    AttLabel->show();
    DefLabel->hide();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::updateRedAreaPosition() {
    if(Rtop > 162){Rtop = 162;}
    redAreaRect = QRect(Rleft, Rtop, 80, 80);
    Rright = Rleft + 80;
    Rbottom = Rtop + 80;
    update();
}

void MainWindow::paintEvent(QPaintEvent *event){
   QPainter painter(this);
   painter.setRenderHint(QPainter::Antialiasing);
   int leftPadding = 30;
   painter.setPen(Qt::gray);
   painter.setBrush(Qt::blue);
   painter.drawRect(leftPadding, 0, 732, 244);

   painter.setPen(Qt::black);
   painter.setBrush(Qt::black);
   painter.drawRect(goalkeeperRect);

   painter.setPen(Qt::red);
   painter.setBrush(Qt::red);
   painter.drawEllipse(redAreaRect);

   painter.setPen(Qt::yellow);
   painter.setBrush(Qt::yellow);
   painter.drawEllipse(redOppRect);
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    if(turn % 2 == 0){
    switch (event->key()) {
    case Qt::Key_Up:
        Rtop -= speed;
        Rbottom -= speed;
        updateRedAreaPosition();
        break;
    case Qt::Key_Down:
        Rtop += speed;
        Rbottom += speed;
        updateRedAreaPosition();
        break;
    case Qt::Key_Left:
        Rleft -= speed;
        Rright -= speed;
        updateRedAreaPosition();
        break;
    case Qt::Key_Right:
        Rleft += speed;
        Rright += speed;
        updateRedAreaPosition();
        break;
    case Qt::Key_Space:
        AttLabel->hide();
        DefLabel->show();
        generateGoalkeeperPosition();
        double overlapArea = calculateOverlapArea(goalkeeperRect, redAreaRect);
        double redAreaSize = redAreaRect.width()*redAreaRect.width()/4*3.14;
        double overlapRatio = overlapArea / redAreaSize;
        if(overlapRatio < 0.2){
            adm++;
        }
        updateScore();
        turn++;
        admtime--;
        victory();
        spt++;
        break;
    }
    update();
    }
    else{
        switch (event->key()) {
        case Qt::Key_Up:
            goalkeeperRect.adjust(0, -speed, 0, -speed);
            break;
        case Qt::Key_Down:
            goalkeeperRect.adjust(0, speed, 0, speed);
            break;
        case Qt::Key_Left:
            goalkeeperRect.adjust(-speed, 0, -speed, 0);
            break;
        case Qt::Key_Right:
            goalkeeperRect.adjust(speed, 0, speed, 0);
            break;
        case Qt::Key_Space:
            generateRedArea();
            DefLabel->hide();
            AttLabel->show();
            double overlapArea = calculateOverlapArea(goalkeeperRect, redOppRect);
            double redAreaSize = redOppRect.width()*redOppRect.width()/4*3.14;
            double overlapRatio = overlapArea / redAreaSize;
            if(overlapRatio < 0.2){
                opp++;
            }
            updateScore();
            turn++;
            opptime--;
            victory();
            spt++;
            break;
        }
        update();
    }
}

void MainWindow::generateGoalkeeperPosition() {
    int minX = 30;
    int maxX = 780;
    int minY = 0;
    int maxY = 244;
    int rectWidth = 300;
    int rectHeight = 100;
    int x, y;
    do {
        x = qrand() % (maxX - minX) + minX;
        y = qrand() % (maxY - minY) + minY;
    } while (x + rectWidth > maxX || y + rectHeight > maxY);
    goalkeeperRect = QRect(x, y, rectWidth, rectHeight);
}

void MainWindow::generateRedArea() {
    int minX = 30;
    int maxX = 780;
    int minY = 0;
    int maxY = 244;
    int rectSize = 80;
    int x, y;
    do {
        x = qrand() % (maxX - minX) + minX;
        y = qrand() % (maxY - minY) + minY;
    } while (x + rectSize > maxX || y + rectSize > maxY);
    redOppRect = QRect(x, y, rectSize, rectSize);
}

double MainWindow::calculateOverlapArea(const QRect &rect1, const QRect &rect2) {
    int x1 = std::max(rect1.left(), Rleft);
    int y1 = std::max(rect1.top(), Rtop);
    int x2 = std::min(rect1.right(), Rright);
    int y2 = std::min(rect1.bottom(), Rbottom);
    if (x1 < x2 && y1 < y2) {
        int width = x2 - x1;
        int height = y2 - y1;
        return width * height;
    } else {
        return 0;
    }
}

void MainWindow::createScoreBox() {
    QLabel *scoreLabel = new QLabel(this);
    scoreLabel->setText("Score");
    scoreLabel->move(390, 260);

    QLabel *admLabel = new QLabel(this);
    admLabel->setText("ADM");
    admLabel->move(280, 300);

    QLabel *oppLabel = new QLabel(this);
    oppLabel->setText("OPP");
    oppLabel->move(530, 300);

    QLabel *turnLabel = new QLabel(this);
    turnLabel->move(300, 400);
    turnLabel->setText("TURN");

    admScoreLabel = new QLabel(this);
    admScoreLabel->setText(QString::number(adm));
    admScoreLabel->setStyleSheet("font-size: 20px; color: blue;");
    admScoreLabel->move(286, 330);

    oppScoreLabel = new QLabel(this);
    oppScoreLabel->setText(QString::number(opp));
    oppScoreLabel->setStyleSheet("font-size: 20px; color: red;");
    oppScoreLabel->move(536, 330);

    turnScoreLabel = new QLabel(this);
    turnScoreLabel->setText(QString::number(kic + 1));
    turnScoreLabel->setStyleSheet("font-size: 20px; color: green;");
    turnScoreLabel->move(500, 400);

    AttLabel = new QLabel(this);
    AttLabel->setStyleSheet("font-size: 50px; color: purple;");
    AttLabel->setText("YOU ATTACK!");
    AttLabel->move(280, 400);
    AttLabel->setFixedSize(800, 200);

    DefLabel = new QLabel(this);
    DefLabel->setStyleSheet("font-size: 50px; color: orange;");
    DefLabel->setText("YOU DEFEND!");
    DefLabel->setFixedSize(800, 200);
    DefLabel->move(280, 400);
}

void MainWindow::updateScore() {
    admScoreLabel->setText(QString::number(adm));
    oppScoreLabel->setText(QString::number(opp));
    turnScoreLabel->setText(QString::number(kic +1));
}

bool MainWindow::victory(){
    bool result = false;
    int flag = 0;
            if(spt % 2 != 0){
                kic++;
            }
            if(kic <= 5){
                if(opp - adm > admtime){
                    result = false;
                    flag++;
                }
                if(adm - opp > opptime){
                    result = true;
                    flag++;
                }
            }

            if(kic >= 6 && spt % 2 != 0){
                if(adm > opp){
                    result = true;
                    flag++;
                }
                if(adm < opp){
                    result = false;
                    flag++;
                }
            }
        if(flag != 0){
        if (result) {
                QMessageBox::information(this, "Victory", "You Win!");
            } else {
                QMessageBox::information(this, "Defeat", "You Lose!");
            }
        }
        return result;
    }
